package cn.edu.ctbu.api01.restcontroller;

import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping(value="/api/student")  /**通用地址前缀**/
public class StudentRestController {
    @Autowired
    private StudentService studentService;

    @GetMapping(value="/list")
    public List<Student> GetAll(){
        return studentService.findAll();
    }
    /**
     * 读取--url:/api/student/get/id
     * @return
     */
    @GetMapping(value="/get/{id}")
    public Student get(@PathVariable Integer id){
        Student student = studentService.findById(id);
        student.setPassword("");
        return student;
    }
    /**
     * 示例：http://localhost:8080/api/student/getbyid/4
     * 其中  4  表示 id
     * @param id
     * @return
     */
    @GetMapping(value="/getbyid/{id}")
    public Student GetById(@PathVariable Integer id){
        return studentService.findById(id);
    }
    /**
     * * 示例：http://localhost:8080/api/student/getbyname/李玲
     * @param name
     * @return
     */
    @GetMapping(value="/getbyname/{name}")
    public List<Student> GetByName(@PathVariable String name){
        return studentService.findByName(name);
    }
    /**
     * 根据 id删除
     * @param id
     * @return
     */
    @DeleteMapping(value="deletebyid/{id}")
    public boolean DeleteById(@PathVariable Integer id){
        studentService.deleteById(id);
        return true;
    }
    /**
     * 增加学生
     * @param student
     * @return
     */
    @PostMapping(value="add")
    public Integer Add(@RequestBody Student student){
        studentService.insert(student);
        return student.getId();
    }
    /**
     * 更新学生的方法
     * @param student
     * @return
     */
    @PutMapping(value="update")
    public Student update(@RequestBody Student student){
        Student student1=studentService.update(student);
        return student1;
    }
    /**
     * 增加学生的方法
     * @param student
     * @return
     */
    @PostMapping(value="/insert")
    public Student insert(@RequestBody Student student){
        Student student1=studentService.insert(student);
        return student1;
    }
    /**
     * 删除学生的方法
     * @param student
     */
    @DeleteMapping(value="/delete")
    public void delete(Student student){
        studentService.delete(student);
    }

    /**
     * 查询学生
     * @param no
     * @param name
     * @param sex
     * @param ageRange
     * @param scoreRange
     * @return
     */
    @GetMapping(value="/search")
    public List<Student> searchStudents(
            @RequestParam(required = false) String no,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer sex,
            @RequestParam(required = false) String ageRange,
            @RequestParam(required = false) String scoreRange
    ) {
        return studentService.searchStudents(no, name, sex, ageRange, scoreRange);
    }
}
